package com.kelvin.paopao.module.peisong.list;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.PeisongModel;
import com.kelvin.paopao.widgets.EmptyLayout;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/31.
 */

public class PeisongListPresenter implements IPeisongListPresenter {
    private int pageIndex = 1;

    private PeisonglistActivity mView;

    public PeisongListPresenter(PeisonglistActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(final boolean isRefresh) {
        pageIndex = 1;
        RetrofitService.getPeisonList(String.valueOf(pageIndex))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        mView.showLoading();
                    }
                }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<PeisongModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<PeisongModel> commonListResponse) throws Exception {
                        mView.hideLoading();
                        if (commonListResponse.getReason().equals("成功") &&
                                commonListResponse.getResult() != null && commonListResponse.getResult().size() != 0) {
                            mView.loadData(commonListResponse.getResult());
                        } else {
                            mView.showEmpty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mView.showNetError(new EmptyLayout.OnRetryListener() {
                            @Override
                            public void onRetry() {
                                getData(isRefresh);
                            }
                        });
                    }
                });
    }

    @Override
    public void getMoreData() {
        pageIndex++;
        RetrofitService.getPeisonList(String.valueOf(pageIndex))
                .subscribe(new Consumer<CommonListResponse<PeisongModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<PeisongModel>
                                               commonListResponse) throws Exception {
                        if (commonListResponse.getReason().equals("成功") &&
                                commonListResponse.getResult() != null && commonListResponse.getResult().size() != 0) {
                            mView.loadMoreData(commonListResponse.getResult());
                        }
                    }
                });
    }

    @Override
    public void requestPeisong(String oId) {
        RetrofitService.requestPeisong(oId).subscribe(new Consumer<CommonListResponse>() {
            @Override
            public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                if (commonListResponse.getReason().equals("成功")) {
                    ToastUtil.showToast(mView, "报名成功");
                } else {
                    ToastUtil.showToast(mView, commonListResponse.getReason());
                }
            }
        });
    }
}
